package com.twu.biblioteca.ui.menus.movies;

import com.twu.biblioteca.items.Movie;
import com.twu.biblioteca.library.Library;
import com.twu.biblioteca.ui.menus.AbstractMenu;
import com.twu.biblioteca.ui.menus.items.ReturnToMainMenuItem;
import com.twu.biblioteca.ui.menus.items.movies.CheckoutMovieMenuItem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Alvi Jonathan Sait
 */
public class ListMoviesMenu extends AbstractMenu {

    public ListMoviesMenu(Library library) {
        super(library);
        addMenuItems();
    }

    @Override
    public String getOutput() {
        StringBuilder builder = new StringBuilder();
        Collection<Movie> movies = getMovies();
        builder.append(movies.size()).append(" movie(s) found").append(System.lineSeparator());
        builder.append("----------------------------------------------------------------------------------------------").append(System.lineSeparator());
        builder.append(Movie.formatString("Movie ID", "Year", "Title", "Director", "Rating")).append(System.lineSeparator());
        builder.append("----------------------------------------------------------------------------------------------").append(System.lineSeparator());
        for (Movie movie : movies) {
            builder.append(movie).append(System.lineSeparator());
        }
        return builder.substring(0, builder.length() - System.lineSeparator().length());
    }

    @Override
    public String getUserPrompt() {
        StringBuilder builder = new StringBuilder(generateMenuItemsOutput());
        builder.append("Please select an option:");
        return builder.toString();
    }

    protected void addMenuItems() {
        addMenuItem(new CheckoutMovieMenuItem());
        addMenuItem(new ReturnToMainMenuItem());
    }

    protected Collection<Movie> getMovies() {
        ArrayList<Movie> movies = new ArrayList<Movie>(getLibrary().getMovies().values());
        Collections.sort(movies);
        return movies;
    }


}
